package random_test;

public class Test_10_6 {
    //27. 移除元素
    public int removeElement(int[] nums, int val) {
        if(nums.length == 0) return 0;
        int left = 0;
        int right = nums.length - 1;
        int tmp = 0;
        while(left < right) {
            while(left < right && nums[left] != val) left++;
            while(left < right && nums[right] == val) right--;
            tmp = nums[left];
            nums[left] = nums[right];
            nums[right] = tmp;
        }
        if(nums[left] != val) return left + 1;
        return left;
    }


    //34. 在排序数组中查找元素的第一个和最后一个位置
    public int[] searchRange(int[] nums, int target) {
        int[] ret = new int[2];
        int start = -1, end = -1;
        ret[0] = start;
        ret[1] = end;
        if(nums.length == 0) return ret;

        int left = 0, right = nums.length - 1;
        int mid = (left + right) / 2;
        while(left <= right) {
            mid = (left + right) / 2;
            if(nums[mid] < target) {
                left = mid + 1;
            } else if(nums[mid] > target) {
                right = mid - 1;;
            } else {
                start = end = mid;
                while(left <= start && nums[start] == target) start--;
                while(end <= right && nums[end] == target) end++;
                ret[0] = start + 1;
                ret[1] = end - 1;
                break;
            }
        }
        return ret;
    }


}
